<?php
// +----------------------------------------------------------------------
// | INPHP
// | Copyright (c) 2023 https://inphp.cc All rights reserved.
// | Author: 幺月儿(https://gitee.com/lulanyin) Email: inphp@qq.com
// | 该文件的开源协议以所在项目的LICENSE文件为准，请遵守开源协议要求
// +----------------------------------------------------------------------
// | 文章标签，全站点，通用
// +----------------------------------------------------------------------
namespace app\cms\http\api;

use app\admin\attributes\auth;
use app\cms\helper\cache;
use app\cms\model\TagsModel;
use Inphp\Core\Object\Message;
use Inphp\Core\Util\Str;

class tags
{
    public function list(?array $params = null): array
    {
        $params = $params ?? getClient()->get;
        //优先从缓存获取
        $cache = cache::get($params, "tags/list");
        if ($cache) {
            //缓存中不为空时，直接返回
            return $cache;
        }

        $db = TagsModel::emptyQuery();

        //行数
        $rows = $db->rows();
        //当前页数
        $page = $params["page"] ?? 1;
        $page = is_numeric($page) && $page > 0 ? ceil($page) : 1;
        //总页数
        $pages = 1;
        //列表数据
        $list = [];
        if ($rows > 0) {
            $total = $params["total"] ?? 30;
            $total = is_numeric($total) && $total > 0 ? $total : 30;
            $total = min($total, 500);
            $pages = ceil($rows / $total);
            $page = $page > $pages ? $pages : $page;
            $offset = $total * ($page - 1);
            $list = $db->orderBy("archives", "desc")->orderBy("id", "desc")->get($total, $offset);
        }
        //返回列表数据，包含行数、页码、总页数、数据列表
        $data = compact("rows", "page", "pages", "list");
        if ($rows > 0) {
            cache::set($params, $data, "tags/list");
        }
        return $data;
    }

    /**
     * 保存标签
     * @return Message
     */
    #[auth("cms/tags")] public function save(): Message
    {
        $tag = POST("tag");
        $tag = !empty($tag) ? Str::trim($tag) : null;
        if (empty($tag)) {
            return httpMessage("缺少标签名称");
        }
        $tag = mb_strlen($tag) > 20 ? mb_substr($tag, 0, 20) : $tag;
        $id = POST("id");
        $id = is_numeric($id) && $id > 0 ? ceil($id) : 0;
        //查重
        $exists = TagsModel::emptyQuery()
            ->where("tag", $tag)
            ->where("id", "!=", $id)
            ->first();
        if (!empty($exists)) {
            return httpMessage("同名标签已存在");
        }
        $db = TagsModel::emptyQuery()->set("tag", $tag);
        $method = "insert";
        if ($id > 0) {
            $db->where("id", $id);
            $method = "update";
        }
        if (!$db->{$method}()) {
            return httpMessage("保存失败，请稍后再试");
        }
        //清除缓存
        cache::clean("tags");
        return httpMessage(0, "保存成功");
    }

    /**
     * 删除
     * @return Message
     */
    #[auth("cms/tags")] public function delete(): Message
    {
        $id = POST("id");
        $id = !empty($id) ? Str::trim($id) : "";
        if (empty($id)) {
            return httpMessage("缺少ID参数");
        }
        $ids = explode(",", $id);
        $idList = [];
        foreach ($ids as $id) {
            if (is_numeric($id) && $id > 0 && !in_array($id, $idList)) {
                $idList[] = $id;
            }
        }
        if (empty($idList)) {
            return httpMessage("ID无效");
        }
        $db = TagsModel::emptyQuery()->whereIn("id", $idList);
        if ($db->delete()) {
            //清除缓存
            cache::clean("tags");
            return httpMessage(0, "已删除{$db->getAffectRows()}行数据");
        }
        return httpMessage("删除失败，请稍后再试");
    }
}